setwd("D:/Master/Data/Oppgave")
close = function(n) return(c(n,n[1]));
files = list.files("data\\weights")
n = length(files)
avgx = matrix(0,200)
avgy = matrix(0,200)

xlists = c()
ylists = c()


for(name in files){
	chaint = readLines(paste("data\\weights\\",name,sep=""),warn=0)
	chain = as.numeric(data.frame(strsplit(chaint,""))$c..)-2

	# convert chain to dx,dy
	dt = 1+(sqrt(2)-1) * (chain%%2)
	t = cumsum(dt)
	Tk = sum(dt)

	
	dx = sign(6-chain)*sign(2-chain)
	dy = -sign(4-chain)*sign(chain)
	x = cumsum(dx)
	y = cumsum(dy)
		
	nx = approx(close(x),n=200)$y
	ny = approx(close(y),n=200)$y
	
	write.table(data.frame(nx,ny),paste("Data\\paths\\normalized\\",name,sep=""))
	write.table(data.frame(x,y),paste("Data\\paths\\standard\\",name,sep=""))


	avgx = avgx + x[1+(0:199)/200*length(x)] / n
	avgy = avgy + y[1+(0:199)/200*length(y)] / n 
}
write.table(data.frame(avgx,avgy),"Data\\paths\\norm_average")





